﻿using System;
using System.Collections.Generic;
using Components;
using System.Text;
using System.Windows.Forms;
using Controls;
using System.Reflection;
using System.Data;

namespace UI.Lib
{
    public class UIHelper
    {
        /// <summary>
        /// 从GridViewRow中得到数据填充实体
        /// </summary>
        /// <param name="o"></param>
        /// <param name="dr"></param>
        public static void FillModelFromDataGridViewRow(object o, DataGridViewRow dr)
        {
            Type oType = o.GetType();
            PropertyInfo[] propertys = oType.GetProperties();

            string name = "";
            object tv = null;
            DataGridView dg = dr.DataGridView;
            DataGridViewColumnCollection cc = dg.Columns;
            foreach (PropertyInfo info in propertys)
            {
                name = info.Name.ToLower();
                if (!cc.Contains(name)) continue;

                tv = dr.Cells[name].Value; //获得控件的值

                CommonUtil.GetInstance.AddValue(info, (null == tv ? "" : tv.ToString()), o);
            }
        }

        public static void FillDataGridViewRowFromDataRow(DataRow dr, DataGridViewRow dgr)
        {
            if (dr == null) return;
            string col = "";

            DataColumnCollection dcc = dr.Table.Columns;
            DataGridViewColumnCollection dvcc = dgr.DataGridView.Columns;
            foreach (DataColumn dc in dcc)
            {
                col = dc.ColumnName.ToLower();

                if (!dvcc.Contains(col)) continue;
                dgr.Cells[col].Value = dr[col];
            }
        }

        /// <summary>
        /// 向属性中添加对应类型的值
        /// </summary>
        /// <param name="info">属性对象</param>
        /// <param name="Value">值</param>
        /// <param name="obj">属性所属对象</param>
        /// <param name="hasKey">Request对象中是否有该属性</param>
        private static void AddValue(PropertyInfo info, String value, Object obj)
        {
            if (string.IsNullOrEmpty(value)) //如果是空
            {
                info.SetValue(obj, null, null);
                return;
            }
            if (info.PropertyType == typeof(String))
            {
                info.SetValue(obj, value, null);
            }
            else if (info.PropertyType == typeof(int?) || info.PropertyType == typeof(int))
            {
                info.SetValue(obj, int.Parse(value), null);
            }
            else if (info.PropertyType == typeof(DateTime?) || info.PropertyType == typeof(DateTime))
            {
                info.SetValue(obj, DateTime.Parse(value), null);
            }
            else if (info.PropertyType == typeof(Decimal?) || info.PropertyType == typeof(Decimal))
            {
                info.SetValue(obj, Decimal.Parse(value), null);
            }
            else if (info.PropertyType == typeof(Double?) || info.PropertyType == typeof(Double))
            {
                info.SetValue(obj, Double.Parse(value), null);
            }
            else if (info.PropertyType == typeof(float?) || info.PropertyType == typeof(float))
            {
                info.SetValue(obj, float.Parse(value), null);
            }
            else if (info.PropertyType == typeof(bool?) || info.PropertyType == typeof(bool))
            {
                info.SetValue(obj, (value != "0"), null);
            }
            else if (info.PropertyType == typeof(Guid?) || info.PropertyType == typeof(Guid))
            {
                if (value != null)
                    info.SetValue(obj, new Guid(value), null);
            }

        }

        /// <summary>
        /// 组织字符串，给出提示信息
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static string TranslateInfo(DataTable dt, string title, int rowCount)
        {
            if (null == dt || 0 == dt.Rows.Count) return null;

            StringBuilder sb = new StringBuilder();
            if (!string.IsNullOrEmpty(title))
            {
                sb.Append(title);
                sb.AppendLine();
                sb.AppendLine();
                sb.AppendLine();
            }
            //翻译标题
            foreach (DataColumn dc in dt.Columns)
            {
                sb.AppendFormat("{0}\t\t", dc.ColumnName);
            }

           
            sb.AppendLine();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (rowCount != 0 && i > rowCount) break; //仅显示多少条

                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    sb.AppendFormat("{0}\t\t", dt.Rows[i][j]);
                }

                sb.AppendLine();
            }

            if (rowCount != 0 && rowCount < dt.Rows.Count)
            {
                sb.Append("……………………");
                sb.AppendLine();
            }

            return sb.ToString();
        }
    }
}
